package ru.spb.osll.myfit.server.handler;

import org.apache.log4j.Logger;
import ru.spb.osll.myfit.common.Define;
import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;
import ru.spb.osll.myfit.common.TextParsingTools;
import ru.spb.osll.myfit.server.parser.PageUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParentVariablesCreator {
  private static Logger LOG = Logger.getLogger(PageLoader.class);
  private PageLoader pageLoader;

  public ParentVariablesCreator(PageLoader loader) {
    pageLoader = loader;
  }

  public HashMap<String, String> getParentVariablesMap(String pageName,
                                                       String m_root) {
    List<String> parentList = getParentPaths(pageName, m_root);
    return getParentVariables(parentList, m_root);
  }

  private List<String> getParentPaths(String pageName, String m_root) {
    LinkedList<String> paths = new LinkedList<String>();
    Matcher m = Pattern.compile(TextParsingTools.ADDRESS_BAR_SEPARATOR_REGEX).matcher(pageName);
    String parent;
    while (m.find()) {
      parent = pageName.substring(0, m.start());
      paths.add(PageUtils.getFullPathToContent(m_root, parent));
    }
    return paths;
  }

  protected HashMap<String, String> getParentVariables(List<String> list,
                                                       String m_root) {
    HashMap<String, String> map = new HashMap<String, String>();
    for (String path : list) {
      try {
        HashMap<String, String> loadedMap = loadPageWithoutParentVariables(
            path, m_root);
        map.putAll(loadedMap);
      } catch (IOException e) {
        LOG.error("File \"" + path + "\" does not exist!");
      }
    }
    return map;
  }

  public HashMap<String, String> loadPageWithoutParentVariables(String fileName, String m_root)
      throws IOException {
    HashMap<String, String> map = new HashMap<String, String>();
    Page page = pageLoader.loadPage(fileName);
    
    for (PageElement element : page.getElements()) {
      PageElementType type = element.getType();
      if(type == PageElementType.DEFINE){
        Define define = (Define)element;
        map.put(define.getNameVariable(), define.getValueVariable());
      }else if (isInclude(type)){
        String includePath = PageUtils.getFullPathToContent(m_root, ((Include) element).getPath());
        try{
          map.putAll(loadPageWithoutParentVariables(includePath, m_root));
        } catch (IOException e){
          LOG.error("File \"" + includePath + "\" does not exist!");
        } 
      }
    }  
    return map;
  }

  private boolean isInclude(PageElementType type) {
    return (type == PageElementType.INCLUDE || type == PageElementType.SET_UP || type == PageElementType.TEAR_DOWN);
  }
}
